package com.mysoft.gfyx.workbench.controller;

import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.common.util.LogUtil;
import com.mysoft.gfyx.workbench.controller.dto.MainAppLoginRequestDto;
import com.mysoft.gfyx.workbench.controller.dto.MainAppLoginResponseDto;
import com.mysoft.gfyx.workbench.service.MainAppService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Objects;

@PubService(value = "/mainAppDataApi", prefix = RequestPrefix.API)
@Tag(name = "主应用-API")
public class MainAppDataApiController {

    @Autowired
    private MainAppService mainAppService;

    @Autowired
    private LogUtil logUtil;

    @PubAction(value = "/getLoginUrl", method = RequestMethod.POST)
    public MainAppLoginResponseDto getLoginUrl(@RequestBody MainAppLoginRequestDto dto) {
        if (dto.getPageUri() == null || Objects.equals(dto.getPageUri(), "")) {
            throw new BusinessLogicException("请传入对应的uri");
        }

        MainAppLoginResponseDto responseDto = new MainAppLoginResponseDto();
        try {
            String url = mainAppService.getLoginUrl(dto.getPageUri());
            responseDto.setUrl(url);
        } catch (Exception e) {
            logUtil.error("单点登录主应用", e);
            responseDto.setUrl("");
        }
        return responseDto;
    }
}
